cmake_minimum_required(VERSION 3.8)
project(unitree_lidar_ros2)

cmake_policy(SET CMP0074 NEW)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic -Wno-unused-parameter)
endif()

# 查找依赖包
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)
find_package(tf2_eigen REQUIRED)
find_package(tf2 REQUIRED)
find_package(pcl_conversions REQUIRED)
find_package(visualization_msgs REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(nav_msgs REQUIRED)  # 新增：用于 Odometry 消息

find_package(PCL REQUIRED COMPONENTS common io filters segmentation search kdtree)
find_package(Boost REQUIRED COMPONENTS system)
find_package(Eigen3 REQUIRED)
find_package(OpenCV REQUIRED)

# ONNX Runtime路径设置
set(ONNXRUNTIME_ROOT_PATH "/home/sl1/Desktop/pytorch_SDK/onnxruntime")
set(ONNXRUNTIME_INCLUDE_DIRS "${ONNXRUNTIME_ROOT_PATH}/include")
set(ONNXRUNTIME_LIB "${ONNXRUNTIME_ROOT_PATH}/lib/libonnxruntime.so")

# 验证ONNX Runtime
if(NOT EXISTS "${ONNXRUNTIME_INCLUDE_DIRS}/onnxruntime_cxx_api.h")
    message(FATAL_ERROR "ONNX Runtime headers not found at: ${ONNXRUNTIME_INCLUDE_DIRS}")
endif()

if(NOT EXISTS "${ONNXRUNTIME_LIB}")
    message(FATAL_ERROR "ONNX Runtime library not found at: ${ONNXRUNTIME_LIB}")
endif()

message(STATUS "Found ONNX Runtime at: ${ONNXRUNTIME_ROOT_PATH}")

# Unitree SDK路径
set(UNITREE_SDK_BASE_DIR "/home/sl1/Desktop/ROS2_SDK/unilidar_sdk2-2.0.4/unitree_lidar_sdk")

if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
    set(UNITREE_LIB_PATH "${UNITREE_SDK_BASE_DIR}/lib/aarch64/libunitree_lidar_sdk.a")
else()
    set(UNITREE_LIB_PATH "${UNITREE_SDK_BASE_DIR}/lib/x86_64/libunitree_lidar_sdk.a")
endif()

# 可执行文件
add_executable(unitree_lidar_ros2_node src/unitree_lidar_ros2_node.cpp)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
# 包含目录
target_include_directories(unitree_lidar_ros2_node 
  PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    "${UNITREE_SDK_BASE_DIR}/include"
    ${PCL_INCLUDE_DIRS}
    ${EIGEN3_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS}
    ${ONNXRUNTIME_INCLUDE_DIRS}
)

# ROS依赖
ament_target_dependencies(unitree_lidar_ros2_node
  rclcpp
  std_msgs
  sensor_msgs
  geometry_msgs
  tf2  # 新增
  tf2_ros
  tf2_geometry_msgs
  tf2_eigen
  pcl_conversions
  visualization_msgs
  cv_bridge
  nav_msgs  # 新增：用于 Odometry 消息
)

# 链接库
target_link_libraries(unitree_lidar_ros2_node
  ${PCL_LIBRARIES}
  ${Boost_LIBRARIES}
  "${UNITREE_LIB_PATH}"
  ${OpenCV_LIBS}
  ${ONNXRUNTIME_LIB}
)

target_compile_definitions(unitree_lidar_ros2_node 
  PRIVATE 
    PCL_NO_PRECOMPILE
)

install(TARGETS unitree_lidar_ros2_node
  DESTINATION lib/${PROJECT_NAME})

# 安装launch文件 - 添加这部分
install(DIRECTORY launch
  DESTINATION share/${PROJECT_NAME}/
  FILES_MATCHING PATTERN "*.py"
)

ament_package()